package UIlayer;
import CtrlLayer.*;
import java.util.Scanner;
import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * Write a description of class Main here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MainUI
{
    private Scanner input;
    private Scanner input1;
    private Scan scan;
    private VareUI vareUI;
    private OrdreUI ordreUI;
    private KundeUI kundeUI;

    /**
     * Constructor for objects of class Main
     */
    private MainUI()
    {
        input = scan.getInstance();
        vareUI = vareUI.getInstance();
        ordreUI = ordreUI.getInstance();
        kundeUI = kundeUI.getInstance();
    }

    private MainUI(String fileName)
    throws IOException
    {
        try
        {
            File file = makeAbsoluteFilename(fileName);
            //input1 = new Scanner(file);
            input = scan.getInstance(file);
        }
        catch(FileNotFoundException e)
        {
            System.out.println("File not found");
        }
        catch(NullPointerException e)
        {
            System.out.println("Something went wrong");
        }
        vareUI = vareUI.getInstance();
        ordreUI = ordreUI.getInstance();
        kundeUI = kundeUI.getInstance();
    }

    private File makeAbsoluteFilename(String filename) throws IOException
    {
        try 
        {
            File file = new File(filename);
            if(!file.isAbsolute()) 
            {
                file = new File(getProjectFolder(), filename);
            }
            return file;
        }
        catch(URISyntaxException e) 
        {
            throw new IOException("Unable to make a valid filename for " + filename);
        }
    }

    private File getProjectFolder() throws URISyntaxException
    {
        String myClassFile = getClass().getName() + ".class";
        if(getClass().getPackage() != null)
        {
            int layerDepth = getClass().getPackage().getName().length() + 1;
            myClassFile = myClassFile.substring(layerDepth,myClassFile.length());
        }
        URL url = getClass().getResource(myClassFile);
        return new File(url.toURI()).getParentFile();
    }

    /**
     * Starter UI'en
     */
    public static void main()
    {
        MainUI UI = new MainUI();
        UI.mainMenu();
    }

    public static void test()
    {
        try
        {
            MainUI UI = new MainUI("test.txt");
            UI.mainMenu();
        }
        catch(IOException e)
        {
            System.out.println("something fucked up");
        }
    }

    /**
     * Menuen hvorfra man kan vælge hvor man går hen
     */
    public void mainMenu()
    {
        boolean exit = false;
        int i = 0;
        while(!exit)
        {
            System.out.println("\n\n1. Ordre menu \n2. Vare menu \n3. Kunde menu \nHvilket som helst andet tal for at forlade menuen");
            switch(getInt()){
                case -1:
                if(i > 10)
                {
                    fuckoff();
                    exit = true;
                    break;
                }
                System.out.println("enter whole number please ");
                i++;
                break;
                case 1:
                ordreUI.ordreMenu();
                break;
                case 2:
                vareUI.vareMenu();
                break;
                case 3:
                kundeUI.kundeMenu();
                break;
                default:
                System.out.println("Exiting");
                exit = true;
                break;
            }
        }
    }

    /**
     * Returnere den næste int der indtastes, hvis det ikke er en int returner den -1
     */
    private int getInt()
    {
        int choice = -1;
        if(input.hasNextInt())
        {
            choice = input.nextInt();
        }
        else
        {
            String whatever = input.nextLine();
        }
        System.out.println();
        return choice;
    }

    /**
     * En fin metode til de narhatte der er træls
     */
    private void fuckoff()
    {
        System.out.println("now look at what you did...");
        try
        {
            Thread.sleep(1000);
        }
        catch(InterruptedException e){}
        for(int i = 0; i < 100; i++)
        {
            System.out.println("just enter a fucking NUMBER!");
            try
            {
                Thread.sleep(100);
            }
            catch(InterruptedException e){}
        }
    }
}